package com.wpzf.ndhc.controller;


import com.wpzf.common.core.utils.ServletUtils;
import com.wpzf.common.core.web.domain.AjaxResult;
import com.wpzf.common.security.service.TokenService;
import com.wpzf.common.security.utils.SecurityUtils;
import com.wpzf.ndhc.utils.WpzfNdhcExcelUtils;
import com.wpzf.ndhc.domain.*;
import com.wpzf.ndhc.service.WpzfNdhcSqthService;
import com.wpzf.system.api.model.LoginUser;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/wpzfndhcsqth")
public class WpzfNdhcSqthController {

    @Autowired
    private TokenService tokenService;

    @Value("${tempath}")
    public String tempath;

    @Autowired
    private WpzfNdhcSqthService wpzfNdhcSqthService;

    //获取已填报批次号
    @RequestMapping(value = "/getPch", method = RequestMethod.POST)
    public AjaxResult getPch(@RequestBody Map<String, Object> param) {
        try {
            LoginUser loginUser = tokenService.getLoginUser(ServletUtils.getRequest());
            param.put("xzqdm", loginUser.getSysUser().getXzqdm());
            List<Map<String, Object>> list = wpzfNdhcSqthService.getPch(param);
            return AjaxResult.success("获取成功", list);
        } catch (Exception ex) {
            return AjaxResult.error("获取失败", ex.toString());
        }
    }

    //根据用户所在行政区查询所有已经提交到国家级节点的地块
    @RequestMapping(value = "/getDkList", method = RequestMethod.POST)
    public AjaxResult getDkList(@RequestBody Map<String, Object> param) {
        try {
            LoginUser loginUser = tokenService.getLoginUser(ServletUtils.getRequest());
            param.put("xzqdm", loginUser.getSysUser().getXzqdm());
            List<Map<String, Object>> list = wpzfNdhcSqthService.getDkList(param);
            Integer cnt = wpzfNdhcSqthService.getDkListCount(param);
            return AjaxResult.success("获取成功", list, cnt);
        } catch (Exception ex) {
            return AjaxResult.error("获取失败", ex.toString());
        }
    }

    //详情
    @RequestMapping(value = "/queryUserByHctbIdxj", method = RequestMethod.POST)
    public AjaxResult queryUserByHctbIdxj(@RequestBody Map<String, Object> param) {
        String jcbh = param.get("jcbh") + "";
        if (jcbh.indexOf(",") != -1) {
            param.put("jcbh", jcbh.split(",")[0]);
        }
        try {
            WpCjTGjjth wpCjTbBaseinfoxjDTO = new WpCjTGjjth();

            //图斑基本信息
            WpCjTbxxbxjNdDTO WpCjTbxxbDTO = wpzfNdhcSqthService.querytbxxByjcbh(param);
            wpCjTbBaseinfoxjDTO.setWpCjTbxxbxjNdDTO(WpCjTbxxbDTO);

            //图斑举证照片
            WpCjTbxxbxjNdDTO WpCjTbxxbxjNdDTO = wpzfNdhcSqthService.querytbxxNdByjcbh(param);
            ModelMap modelMap = new ModelMap();
            if (WpCjTbxxbxjNdDTO != null) {
                param.put("sfzz", "自主提取".equals(WpCjTbxxbxjNdDTO.getGjxfhzztq()) ? "1" : "0");
                WpCjGsinfolyNdDTO gsinfoly = wpzfNdhcSqthService.getGsinfoly(param);
                wpCjTbBaseinfoxjDTO.setWpCjGsinfolyNdDTO(gsinfoly);
                if ("0".equals(gsinfoly.getInfoly())) {
                    modelMap = wpzfNdhcSqthService.getShowImgsnew(param);
                } else if ("1".equals(gsinfoly.getInfoly())) {
                    List<String> list = new ArrayList<>();
                    list.add(wpzfNdhcSqthService.getInfo(param));
                    modelMap.put("data", list);
                }
            } else {
                modelMap = wpzfNdhcSqthService.getShowImgs(param);
            }

            wpCjTbBaseinfoxjDTO.setModelMap(modelMap);

            //获取地块信息
            List<WpCjDkxxbxjNdDTO> dkxxbxj2023DTOS = wpzfNdhcSqthService.querydkxxBytbbh(param);
            wpCjTbBaseinfoxjDTO.setWpCjDkxxbxjNdDTOList(dkxxbxj2023DTOS);

            //获取图斑截图
            List<WpCjTbjtNdDTO> wpCjTbjt2023DTOS = wpzfNdhcSqthService.querytbjtBytbbh(param);
            wpCjTbBaseinfoxjDTO.setWpCjTbjtNdDTOList(wpCjTbjt2023DTOS);

            //获取市级审核信息
            WpCjTbhcjgshijNdDTO wpCjTbhcjgshijNdDTO = wpzfNdhcSqthService.queryTbhcjgshijBytbbh(param);
            wpCjTbBaseinfoxjDTO.setWpCjTbhcjgshijNdDTO(wpCjTbhcjgshijNdDTO);

            //获取省级审核信息
            WpCjTbhcjgsjNdDTO wpCjTbhcjgsjNdDTO = wpzfNdhcSqthService.queryTbhcjgsjBytbbh(param);
            wpCjTbBaseinfoxjDTO.setWpCjTbhcjgsjNdDTO(wpCjTbhcjgsjNdDTO);

            return AjaxResult.success("获取成功", wpCjTbBaseinfoxjDTO);
        } catch (Exception ex) {
            return AjaxResult.error("获取失败", ex.toString());
        }
    }

    @RequestMapping(value = "/thDk", method = RequestMethod.POST)
    public AjaxResult sqth(@RequestBody Map<String, Object> param) {
        try {
            LoginUser loginUser = tokenService.getLoginUser(ServletUtils.getRequest());
            param.put("sqr", loginUser.getUsername());
            param.put("sqrxm", loginUser.getSysUser().getNickName());
            Integer rest = wpzfNdhcSqthService.thDk(param);
            if (rest > 0) {
                return AjaxResult.success("退回 成功", rest);
            } else {
                return AjaxResult.error("退回 失败", rest);
            }
        } catch (Exception ex) {
            return AjaxResult.error("获取失败", ex.toString());
        }
    }

    //导出地块列表
    @RequestMapping(value = "/exportGjjThDkList", method = RequestMethod.POST, produces = {"application/json;charset=UTF-8"})
    public AjaxResult exportGjjThDkList(@RequestBody Map<String, Object> param, HttpServletResponse response) {
        LoginUser loginUser = tokenService.getLoginUser(ServletUtils.getRequest());
        param.put("xzqdm", loginUser.getSysUser().getXzqdm());
        String rest = "";
        String msg = "";
        WpzfNdhcExcelUtils excel = new WpzfNdhcExcelUtils();
        List<String> Colomnlist = new ArrayList<>();
        Colomnlist.clear();
        Colomnlist.add("序号");
        Colomnlist.add("地块编号");
        Colomnlist.add("批次号");
        Colomnlist.add("行政区代码");
//        Colomnlist.add("申请人员");
        Colomnlist.add("申请人姓名");
        Colomnlist.add("申请退回原因");
        Colomnlist.add("申请时间");
        Colomnlist.add("退回平台");
//        Colomnlist.add("审核人");
        Colomnlist.add("审核人姓名");
        Colomnlist.add("审核意见");
        Colomnlist.add("审核意见说明");
        Colomnlist.add("审核时间");

        String operName = SecurityUtils.getUsername();
        param.put("username", operName);
        //获取图斑列表
        List<WpCjGjjThDkNdDTO> list = wpzfNdhcSqthService.queryGjjThDkList(param);
        rest = excel.exportGjjThDkList(response, "国家级退回地块列表", list, Colomnlist, tempath);
        if (rest.contains("error")) {
            msg = "导出失败";
        } else {
            msg = "导出成功";
        }
        return AjaxResult.success(msg, rest);
    }


}
